package com.itHsuSE.demo5;

/**
 * @author Hsu琛君珩
 * @version 1.0
 */
public class Test4 {
    public static void main(String[] args) {
        Person person=new Person("tang",new Horse());
        person.walk();
        person.swim();
    }
}

class Person{
    private String name;
    private Vehicle vehicle;

    public Person(String name, Vehicle vehicle) {
        this.name = name;
        this.vehicle = vehicle;
    }

    public void walk(){
        if(!(vehicle instanceof Horse)){
            vehicle=Factory.getHorse();
        }
        vehicle.work();
    }

    public void swim(){
        if(!(vehicle instanceof Boat)){
            vehicle=Factory.getBoat();
        }
        vehicle.work();
    }
}

class Factory{
    private static Horse horse=new Horse();
    public static Horse getHorse(){
        return horse;
    }
    public static Boat getBoat(){
        return new Boat();
    }
}

class Boat implements Vehicle{
    @Override
    public void work() {
        System.out.println("游泳");
    }
}

class Horse implements Vehicle{
    @Override
    public void work() {
        System.out.println("走路");
    }
}

interface Vehicle{
    public void work();
}